package org.webshop.frontend.servlets;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.webshop.backend.dbahif.DBAccess;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private DBAccess dba;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
    	// TODO Auto-generated method stub
    	super.init(config);
        dba = DBAccess.getInstance();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect("views/Login.jsp");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("views/Login.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String username = request.getParameter("username");
	        String password = request.getParameter("password");
	        String errMsg = "";
	        HttpSession session = request.getSession();
	        try {
	            if (username == null
	                    || password == null) {
	                session.setAttribute("errorLogin", errMsg);
	                errMsg = "This error should never occur1";
	                processRequest(request, response);
	            } else if (username.trim().isEmpty()) {
	                errMsg = "Please enter username!";
	            }
	            if (password.trim().isEmpty()) {
	                errMsg += "Please enter password!";
	            }
	            if (!errMsg.isEmpty()) {
	                session.setAttribute("errorLogin", errMsg);
	                processRequest(request, response);
	            }
            if (!dba.doesUserExist(username)) {
	                errMsg = "Unknown username!";
	            }
	            if (!dba.checkPassword(username, password)) {
	                errMsg = "Login failed";
	            }
	            if (errMsg.isEmpty()) {
	                response.sendRedirect("");
	            } else {
	                session.setAttribute("errorLogin", errMsg);
	                processRequest(request, response);
	            }
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
	}

}
